<?php
namespace Crm\Domain;

use Crm\Common\MetaData as MetaData;
use Crm\Common\Util as Util;
use Crm\Common\Pager as Pager;

class Company
{
    /**
     * ado连接。
     *
     * @var object
     * @access pdo
     */
    public $pdo;


    public function __construct()
    {
        $this->pdo = \PhalApi\DI()->pdo;
    }

	/* 构造组织机构HTML
	 * param $parent_id int 被选中的项
	 * param $orgstruct_id int 当前树节点,即上下文节点(通过该节点生成Tree,可传入用户所在公司ID $this->session->company_id),用于确定树的范围
	 */
	public function getOrgSelectHtml($parent_id,$orgstruct_id,$type='department',$with='',$event=''){
        $with = ($with>0)?'data-width="'.$with.'"':'';
        if($type=='company'){
            $selectstr = '<select name="parent_id" id="parent_id" class="form-control" '.$with.' '.$event.'>';
            if($this->session->isadmin)$selectstr .= '<option value="0">顶级</option>';
            //如果是顶级部门,则过滤掉自己管理的组织结构树
            if($parent_id==0) {
                $sql = "select * from sys_orgstruct where flag=1 and type = 'company' and id<>{$orgstruct_id}";// and (type='company')";
            }else{
                $sql = "select * from sys_orgstruct where flag=1 and type = 'company'";// and (type='company')";
            }
            $info = $this->pdo->getAll($sql);
            $selectstr .= $this->getOrgStructOption($info, 0, 1, $parent_id);

            $selectstr .="</select>";
        }elseif($type=='department'){
            $selectstr = '<select name="parent_id" id="parent_id" class="form-control" '.$with.' '.$event.'>';
            $selectstr .= '<option value="0">请选择</option>';

            $top_orgstruct = $this->getSupOrgStructInfoById($orgstruct_id,1,'company');//获取当前节点的顶级公司节点
            //todo: $top_orgstruct_parent_id 后期需要根据用户的身份,确定该组织结构
            $top_orgstruct_parent_id = $top_orgstruct['parent_id']; //生成组织结构树的开始节点,该值一般情况下都为0,因为获取的是顶级公司节点
            $info = $this->getOrgStruct2($top_orgstruct['id'],1,$type);

            $selectstr .= $this->getOrgStructOption($info,$top_orgstruct_parent_id,1,$parent_id);
            $selectstr .="</select>";
        }else{
            $selectstr = '<select name="parent_job_id" id="parent_job_id" class="form-control" '.$with.' '.$event.'>';
            $selectstr .= '<option value="0">请选择</option>';

            $top_orgstruct = $this->getSupOrgStructInfoById($orgstruct_id,1,'company');//获取当前节点的顶级公司节点
            $top_orgstruct_parent_id = $top_orgstruct['parent_id']; //生成组织结构树的开始节点,该值一般情况下都为0,因为获取的是顶级公司节点
            $orgStructInfo = $this->getSubOrgInfo($top_orgstruct['id']);//获取该公司下的所有岗位
            if($orgStructInfo) {
                $sql = "select * from sys_orgstruct where id in (0{$orgStructInfo['sys_poststation']})";
                $info = $this->pdo->getAll($sql);
                $selectstr .= $this->getJobStructOption($info, $top_orgstruct_parent_id, 1, $parent_id);
            }
            $selectstr .="</select>";
        }
		return $selectstr;
	}

    /* HTML-构造当前部门、岗位*/
    public function  getPoststationSelectHtmlByDepartmentId($department_id,$job_id){
        $jobList = $this->getPoststationByDepartmentId($department_id);
        $selectstr = '<select name="orgstruct_id" id="orgstruct_id" class="form-control">';
        $selectstr .= '<option value="0">请选择</option>';
        foreach ($jobList as $item) {
            if($item['id']==$job_id) {
                $selectstr .= '<option value="' . $item['id'] . '" selected="selected">' . $item['org_name'] . '</option>';
            }else{
                $selectstr .= '<option value="' . $item['id'] . '">' . $item['org_name'] . '</option>';
            }
        }
        $selectstr .="</select>";
        return $selectstr;
    }

	/* 生成组织结构选项 */
    private function getOrgStructOption($tree,$parent_id,$level,$current_id){
		$selectstr = "";
		foreach ($tree as $k=>$v){
			if($v["parent_id"]==$parent_id){
				$showtype = "";
				for ($i=1;$i<$level;$i++){
					$showtype .= '&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				$showtype .= '|--';

				if($current_id==$v["id"]){
					$selectstr .= '<option  value="'.$v["id"].'" selected>'.$showtype.$v["org_name"].'</option>';
				}else{
					$selectstr .= '<option value="'.$v["id"].'">'.$showtype.$v["org_name"].'</option>';
				}
				$j=0;
				foreach ($tree as $k1=>$v1){
					if($v1["parent_id"]==$v["id"]){
						$j=1;break;
					}
				}
				if($j>0){
					unset($tree[$k]);
					$selectstr .= $this->getOrgStructOption($tree,$v["id"],$level+1,$current_id);
				}
			}
		}
		return $selectstr;
	}

	/* 生成岗位结构选项 */
    private function getJobStructOption($tree,$parent_id,$level,$current_id){
        $selectstr = "";
        foreach ($tree as $k=>$v){
            if($v["parent_job_id"]==$parent_id){
                $showtype = "";
                for ($i=1;$i<$level;$i++){
                    $showtype .= '&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                $showtype .= '|--';

                if($v['type']=='job') {
                    $deptInfo = $this->getJobParentInfo($v["id"]);
                    if ($current_id == $v["id"]) {
                        $selectstr .= '<option  value="' . $v["id"] . '" selected>' . $showtype . $v["org_name"].' - '.$deptInfo['org_name'].'</option>';
                    } else {
                        $selectstr .= '<option value="' . $v["id"] . '">' . $showtype . $v["org_name"].' - '.$deptInfo['org_name'].'</option>';
                    }
                }
                $j=0;
                foreach ($tree as $k1=>$v1){
                    if($v1["parent_job_id"]==$v["id"]){
                        $j=1;break;
                    }
                }
                if($j>0){
                    unset($tree[$k]);
                    $selectstr .= $this->getJobStructOption($tree,$v["id"],$level+1,$current_id);
                }
            }
        }
        return $selectstr;
    }

    /*数据形式形成树*/
    public function getSubCyOrgListByData($tree,$orgid){
        $jsontree = '[';
        $i = 0;
        foreach ($tree as $k=>$v){
            if($v["parent_id"]==$orgid){
                $type = "company";
                if($v["type"]=='department')$type="folder";
                if($v["type"]=='job')$type="file";
                $jsontree .= '{"id":'.$v["id"].',"text":"'.$v["org_name"].'","org_id":'.$v["org_id"].',"parent_id":'.$v["parent_id"].',"type":"'.$type.'"';
                $j=0;
                foreach ($tree as $k1=>$v1){
                    if($v1["parent_id"]==$v["id"]){
                        $j=1;break;
                    }
                }
                if($j>0){
                    $jsontree .= ',"expanded": false,"children":';
                    unset($tree[$k]);
                    $jsontree .= $this->getSubCyOrgListByData($tree,$v["id"]);
                }
                $i = 1;
                $jsontree .= '},';
            }
        }
        if($i==1){
            $jsontree = substr($jsontree,0,-1);
        }
        $jsontree .=']';
        return $jsontree;
    }

    /* 根据部门id获取岗位数据 */
    public function getPoststationByDepartmentId($department_id){
        $sql = "select id,org_id,org_name,parent_id,parent_job_id,`type` from sys_orgstruct where `type`='job' and parent_id=".$department_id;
        $rs = $this->pdo->getAll($sql);
        return $rs;
    }

	/* 获取当前组织机构[但前节点的组织结构:顶级->二级->三级...] */
	public function getOrgStruct($orgstruct_id,$level=1,$orgStr=array()){
		$orgStr = "";
		$sql = "select org_id,org_name,parent_id,parent_job_id,type from sys_orgstruct where id=".$orgstruct_id;
		$orgStruct = $this->pdo->getRow($sql);
		$orgStr[$level]['org_name'] = $orgStruct['org_name'];
        $orgStr[$level]['org_type'] = $orgStruct['type'];
		if($orgStruct['parent_id']>0){
            $level++;
            $as = $this->getOrgStruct($orgStruct['parent_id'],$level,$orgStr);
            $orgStr = array_merge($orgStr,$as);
		}
		return $orgStr;
	}

    /* 获取当前组织机构[公司、部门、岗位]下的 所有下级节点 */
    public function getOrgStruct2($orgstruct_id,$level=1,$type='department'){
        if(isset($orgstruct_id) && $orgstruct_id>0) {
            //$orgStr = array();
            $sql = "select id,org_id,org_name,parent_id,parent_job_id,type,relate_table,flag from sys_orgstruct where flag=1 and id=" . $orgstruct_id;
            $orgStruct = $this->pdo->getRow($sql);
            $orgStr[$level]['id'] = $orgStruct['id'];
            $orgStr[$level]['org_name'] = $orgStruct['org_name'];
            $orgStr[$level]['org_id'] = $orgStruct['org_id'];
            $orgStr[$level]['type'] = $orgStruct['type'];
            $orgStr[$level]['parent_id'] = $orgStruct['parent_id'];
            $orgStr[$level]['parent_job_id'] = $orgStruct['parent_job_id'];
            $orgStr[$level]['relate_table'] = $orgStruct['relate_table'];
            $orgStr[$level]['flag'] = $orgStruct['flag'];
            //判断该节点是否有下级
            if ($type == 'job') {
                $sql = "select id,org_id,org_name,parent_id,parent_job_id,type,relate_table,flag from sys_orgstruct where flag=1 and type='job' and parent_job_id=" . $orgstruct_id;
            } else {
                $sql = "select id,org_id,org_name,parent_id,parent_job_id,type,relate_table,flag from sys_orgstruct where flag=1 and type!='job' and parent_id=" . $orgstruct_id;
            }
            $orgStruct2 = $this->pdo->getAll($sql);
            if (count($orgStruct2) > 0) {
                foreach ($orgStruct2 as $key => $item) {
                    $level++;
                    $as = $this->getOrgStruct2($item['id'], $level, $type);
                    $orgStr = array_merge($orgStr, $as);
                }
            }
            return $orgStr;
        }else{
            return '数据错误';
        }
    }

    /* 获取该组织机构[公司、部门]下的岗位节点列表 */
    public function getJobStructById($orgstruct_id){
        $sql = "select id,org_id,org_name,parent_id,parent_job_id,type,relate_table,flag 
from sys_orgstruct where flag=1 and type='job' and parent_id={$orgstruct_id}";
        $job = $this->pdo->getAll($sql);
        $jobList = array();
        foreach ($job as $item){
            $list = $this->getOrgStruct2($item['id'],1,'job');
            print_r($item['id']);
            //$jobList = array_merge($jobList, $list);
        }
        return $jobList;
    }

	/* 根据org_id，获取orgstruct_id */
	public function getOrgStructId($org_id){
		$sql = "select id from sys_orgstruct where org_id=".$org_id;
		$orgstruct = $this->pdo->getRow($sql);
		return $orgstruct['id'];
	}

    /* 根据组织id，获取组织名称 */
	public function getOrgStructNameById($orgstruct_id){
        $sql = "select id,org_name from sys_orgstruct where id=".$orgstruct_id;
        $orgstruct = $this->pdo->getRow($sql);
        return $orgstruct['org_name'];
    }

    /* 根据组织id，获取组织信息 */
    public function getOrgStructInfoById($orgstruct_id){
        $sql = "select id,org_name,org_id,type,parent_id,parent_job_id,relate_table,flag from sys_orgstruct where id=".$orgstruct_id;
        $orgstruct = $this->pdo->getRow($sql);
        return $orgstruct;
    }

    /* 根据组织id，获取父亲parent_id
     * param $id String orgstruct_id
     * param $is_top int 是否顶级id,1:顶级,2:上一级
     */
    public function getOrgStructParentIdById($orgstruct_id,$is_top=1,$type='company'){
        $rlt = $this->getSupOrgStructInfoById($orgstruct_id,$is_top,$type);
        return $rlt['id'];
    }

    /* 根据组织id,获取上级或顶级[公司或部门]组织机构信息
     * param $id String orgstruct_id
     * param $is_top int 是否顶级id,1:顶级,2:上一级
     * param $type string 返回上级类型,company:公司,department:部门
     */
    public function getSupOrgStructInfoById($orgstruct_id,$is_top=1,$type='company',$node_type=''){
        $sql = "select id,org_name,org_id,parent_id,type,parent_job_id from sys_orgstruct where id=" . $orgstruct_id . " and flag=1";
        $orgInfo = $this->pdo->getRow($sql);
        if(!isset($orgInfo['id'])){ return '没有相关信息';}
        if($is_top==1){
            if($type=='company' || $type=='department') {
                //获取顶级[公司、部门]
                if ($orgInfo['parent_id'] == 0) {
                    return $orgInfo;
                } else {
                    return $this->getSupOrgStructInfoById($orgInfo['parent_id'], $is_top, $type);
                }
            }else{
                //获取顶级[岗位]
                if ($orgInfo['parent_job_id'] == 0) {
                    return $orgInfo;
                } else {
                    return $this->getSupOrgStructInfoById($orgInfo['parent_job_id'], $is_top, $type);
                }
            }
        }else{
            if(($orgInfo['type']==$type && $type=='company') || ($orgInfo['type']==$type && $type=='department') ){
                //如果传入节点为department类型,则直接返回   如果传入节点为job类型,则需要再获取上一级
                $parent_id = $orgInfo['parent_id'];
                if($node_type=='job')$parent_id = $orgInfo['id'];
                if($orgInfo['type']=='company')$parent_id = $orgInfo['id'];//如果自己是公司,则直接返回自己的信息
                //获取上级[公司、部门]
                $sql = "select count(*) as cnt from sys_orgstruct where id=" . $parent_id . " and flag=1";
                $cnt = $this->pdo->getRow($sql);
                if($cnt['cnt']>0){
                    $sql = "select id,org_name,org_id,parent_id,type from sys_orgstruct where id=" . $parent_id . " and flag=1";
                    $orgInfo = $this->pdo->getRow($sql);
                    return $orgInfo;
                }else{
                    return $orgInfo;
                }
            }elseif($orgInfo['type']==$type && $type=='job'){
                $parent_id = $orgInfo['parent_job_id'];
                //获取上级[岗位]
                $sql = "select count(*) as cnt from sys_orgstruct where id=" . $parent_id . " and flag=1";
                $cnt = $this->pdo->getRow($sql);
                if($cnt['cnt']>0){
                    $sql = "select id,org_name,org_id,parent_id,type from sys_orgstruct where id=" . $parent_id . " and flag=1";
                    $orgInfo = $this->pdo->getRow($sql);
                    return $orgInfo;
                }else{
                    return $orgInfo;
                }
            }else{
                $node_type = $orgInfo['type'];
                return $this->getSupOrgStructInfoById($orgInfo['parent_id'],$is_top,$type,$node_type);
            }
        }
    }

    /* 部门级-递归获取组织下的所有下级机构-list */
    public function getSubOrgList($orgstruct_id,&$orglist=array()){
        $sql = "select id,org_name,type from sys_orgstruct where parent_id=".$orgstruct_id." and type<>'job'";
        $info = $this->pdo->getAll($sql);
        //$orglist = '';
        if(count($info)>0){
            foreach ($info as $k=>$v){
                //$orglist .= $v['id'].",";
                //$orglist .= $this->getSubOrgList($v["id"]);
                $orglist[] = $v;
                $this->getSubOrgList($v["id"],$orglist);
            }
        }
        return $orglist;
    }

    /* 部门级-递归获取组织下的所有下级机构:组织机构 岗位数据 部门数据ID列表
	 * sys_poststation、sys_department存放org_id,sys_orgstruct对应主键id
     * Array ( [sys_poststation] => ,18,20,21 [sys_orgstruct] => ,55,58,57,59 [sys_department] => ,19 )
	 */
    public function getSubOrgInfo($orgstruct_id){
        $result = array();
        $info=$this->pdo->getAll("select * from sys_orgstruct where parent_id=".$orgstruct_id);
        if($info){
            foreach ($info as $k => $v) {
                $result[$v['relate_table']].=",".$v['id'];
                $result['sys_orgstruct'].=",".$v['id'];
                $t=$this->getSubOrgInfo($v['id']);
                if($t){
                    foreach ($t as $k1 => $v1) {
                        $result[$k1].=$v1;
                    }
                }
            }
        }
        return $result;
    }

    /* 岗位级-根据岗位ID获取该岗位下的员工 */
    public function getPersonListByJobId($job_id){
        $job_id = intval($job_id);
        $sql = "select id,user_name,user_type,true_name from sys_user where orgstruct_id={$job_id} and flag=1 and active=1";
        $rlt = $this->pdo->getAll($sql);
        return $rlt;
    }

    /* 岗位级-递归获取的上级部门信息 */
    public function getJobParentInfo($orgstruct_id){
        $sql = "select id,org_id,org_name,type,parent_id from sys_orgstruct where id=".$orgstruct_id;
        $orgInfo = $this->pdo->getRow($sql);
        if(isset($orgInfo['id']) && $orgInfo['type']=='job'){
            $orgInfo = $this->getJobParentInfo($orgInfo["parent_id"]);
        }
        return $orgInfo;
    }

    /* 岗位级-递归获取该岗位下的所有下级岗位-list */
    public function getSubRoleList($orgstruct_id,&$orglist=array()){
        $sql = "select id from sys_orgstruct where parent_job_id=".$orgstruct_id;
        $info = $this->pdo->getAll($sql);
        //$orglist = '';
        if(count($info)>0){
            foreach ($info as $k=>$v){
                $orglist[] = $v['id'];
                $this->getSubRoleList($v["id"],$orglist);
            }
        }
        return $orglist;
    }

    /* 岗位级-根据机构ID,获取下面的一级岗位 */
    public function getSubJobInfoById($orgstruct_id){
        $sql = "select id,org_name,org_id,parent_id,type from sys_orgstruct where type='job' and parent_id={$orgstruct_id} and flag=1";
        $role = $this->pdo->getRow($sql);
        if(isset($role['id']) && $role['id']>0) {
            return $role;
        }else{
            return array('id'=>0);
        }
    }

    //级联删除公司、部门、岗位等信息
    function delOrgstruct($relate_table,$org_id,$orgstruct_id){
        //判断是否有下级岗位
        $sql = "select count(id) as cnt from sys_orgstruct where parent_id={$orgstruct_id} and flag=1";
        $count = $this->pdo->getRow($sql);
        if($count['cnt']>0) {
            $result['res'] = 0;
            $result['msg'] = '该公司下存在子公司或部门';
        }else{
            //判断下面是否有用户
            //$sql = "select count(id) as cnt from sys_user where "
            $data['flag'] = 0;
            $rlt1 = $this->pdo->update($data,'sys_orgstruct', ' id='.$orgstruct_id);
            $rlt2 = $this->pdo->update($data,$relate_table, ' id='.$org_id);
            if($rlt2){
                $result['res'] = 1;
                $result['msg'] = '';
            }
        }
        return $result;
    }

	/********* 公司相关  ********/
    /* 公司检索 */
    public function getCompanyBySearch(){
        if($this->session->isadmin) {
            if (isset($_POST['org_name'])) {
                $where = "and flag=1 and org_name like '%" . $_POST['org_name'] . "%'";
            } else {
                $where = "and flag=1 and parent_id=0";
            }
        }else{
            $where = "and flag=1 and id=".$this->session->company_id;
        }
        $sql = "select * from sys_orgstruct where 1=1 ".$where;
        if($_SESSION['isadmin']==1){//管理员获取所有公司
            $info = $this->pdo->getAll($sql);
        }elseif ($_SESSION['userRelateInfo']['ISALLHSUSER']==-2){//获取能操作的公司
            $info = $this->pdo->getAll($sql);
        }else{//获取自己所在公司
            //$info = $this->soap->getSqlidInfo('getHsuserByMyUserId',$params);
            $info = $this->pdo->getAll($sql);
        }
        return $info;
    }

    /** 公司详细 */
    public function getCompany($company_id){
        $sql = "select * from sys_company where id=".$company_id;
        $rs = $this->pdo->getRow($sql);
        return $rs;
    }

    /* save公司 */
    public function saveCompany($data,$act='add'){
        if($act=='add') {
            unset($data['id']);
            $insert_company_id = $this->pdo->add($data, 'sys_company');
            if ($insert_company_id) {
                //新增组织结构信息
                $org = array(
                    'org_name' => $data['name'],
                    'type' => 'company',
                    'parent_id' => $data['parent_id'],
                    'flag' => 1,
                    'relate_table' => 'sys_company',
                    'org_id' => $insert_company_id);
                $insert_id = $this->pdo->add($org, 'sys_orgstruct');
            }

            if($insert_id>0 || $insert_company_id>0) {
                $rlt['res'] = $insert_id;
                $rlt['msg'] = "";
            }else{
                $rlt['res'] = 0;
                $rlt['msg'] = "";
            }
        }else{
            $update_company_id = $this->pdo->update($data,'sys_company','id='.$data["id"]);
            if($update_company_id){
                //编辑组织结构信息
                $org = array(
                    'org_name'=>$data['name'],
                    'type'=>'company',
                    'parent_id'=>$data['parent_id'],
                    'flag'=>1);
                $update_id = $this->pdo->update($org,'sys_orgstruct',"relate_table='sys_company' and org_id=".$data["id"]);
            }
            if($update_id>0 || $update_company_id>0) {
                $rlt['res'] = 1;
                $rlt['msg'] = "";
            }else{
                $rlt['res'] = 0;
                $rlt['msg'] = "";
            }
        }
        return $rlt;
    }

    /********* 部门相关  ********/
    /*  save部门 */
    public function saveDepartment($record,$act='add'){
        if($act=='add'){
            unset($record['id']);
            //新增部门信息sys_department
            $result['org_id'] = $this->pdo->add($record,'sys_department');
            //新增组织结构信息
            $org = array('org_name'=>$record['name'],
                'type'=>'department',
                'parent_id'=>$record['parent_id'],
                'flag'=>1,
                'relate_table'=>'sys_department',
                'org_id'=>$result['org_id']);
            $result['orgstruct_id'] = $this->pdo->add($org,'sys_orgstruct');
            return $result;
        }else{
            $rlt1 = $this->pdo->update($record,'sys_department','id='.$record['id']);//修改部门表信息
            $org = array('org_name'=>$record['name'],'parent_id'=>$record['parent_id']);
            $rlt2 = $this->pdo->update($org,'sys_orgstruct'," id=".$record['orgstruct_id']);
            return $rlt2?$rlt2:$rlt1;
        }
    }

    /* 获取部门详细信息 */
    public function getDepartment($department_id){
        $sql = "select * from sys_department where id=".$department_id;
        $rs = $this->pdo->getRow($sql);
        return $rs;
    }

    /********* 岗位相关  ********/
    /* save岗位 */
    public function saveJob($record,$act='add'){
        if($act=='add'){
            unset($record['id']);
            //新增部门信息sys_department
            $result['org_id'] = $this->pdo->add($record,'sys_poststation');
            //新增组织结构信息
            $org = array('org_name'=>$record['name'],
                'type'=>'job',
                'parent_id'=>$record['parent_id'],//所属部门
                'parent_job_id'=>$record['parent_job_id'],//上级岗位
                'flag'=>1,
                'relate_table'=>'sys_poststation',
                'org_id'=>$result['org_id']);
            $result['orgstruct_id'] = $this->pdo->add($org,'sys_orgstruct');
            return $result;
        }else{
            $rlt1 = $this->pdo->update($record,'sys_poststation','id='.$record['id']);
            $org = array('org_name'=>$record['name'],
                'parent_id'=>$record['parent_id'],
                'parent_job_id'=>$record['parent_job_id']//上级岗位
            );
            $rlt2 = $this->pdo->update($org,'sys_orgstruct',"relate_table='sys_poststation' and org_id=".$record['id']);
            return $rlt2?$rlt2:$rlt1;
        }
    }

    /* 获取岗位详细信息 */
    public function getJob($poststation_id){
        $sql = "select * from sys_poststation where id=".$poststation_id;
        $rs = $this->pdo->getRow($sql);
        return $rs;
    }

    /* 获取岗位树 */
    public function getJobBySearch($parent_id=0,$parent_job_id=0,$level=0){
        $treeInfo = '<ul class="filetree">';
        if($parent_id>0) {
            $sql = "select id,org_name,parent_id,parent_job_id from sys_orgstruct where flag=1 and type='job' and parent_id={$parent_id} and parent_job_id={$parent_job_id}";
        }else{
            $sql = "select id,org_name,parent_id,parent_job_id from sys_orgstruct where flag=1 and type='job' and parent_job_id={$parent_job_id}";
        }
        $info = $this->pdo->getAll($sql);
        //$treeInfo .=count($info).$sql;
        if(count($info)>0) {
            //递归推出条件
            foreach ($info as $key => $item) {
                //获取该岗位下的员工
                $personList = $this->getPersonListByJobId($item['id']);
                $personListUrl = '';
                if(count($personList)>0)$personListUrl .='(';
                foreach ($personList as $personItem){
                    $personListUrl .='<a href="/sys_user/edit?id='.$personItem['id'].'&orgstruct_id='.$item['id'].'&department_id='.$item['parent_id'].'">'.$personItem['true_name'].'、</a>';
                }
                if(count($personList)>0)$personListUrl .=')';
                //<a class=\"opt\" rel=\"".$item['id']."\" onclick=\"auth('".$item['id']."')\" href=\"javascript:void(0)\">修改</a><a class=\"opt\" onclick=\"auth('".$item['id']."')\">删除</a>
                $treeInfo .= "<li><span class='file' rel=".$item['id'].">".$item['org_name'].$personListUrl."<span class=\"control\" id=\"control_file".$item['id']."\" style=\"display: none;\"> <a class=\"opt\" onclick=\"auth('".$item['id']."')\" href=\"javascript:void(0)\">授权</a></span></span>";
                $sql = "select * from sys_orgstruct where flag=1 and type='job' and parent_job_id={$item['id']}";
                //$treeInfo .= $sql;
                $rlt = $this->pdo->getAll($sql);
                if(count($rlt)>0) {
                    $treeInfo .= $this->getJobBySearch(0,$item['id'],$level+1);
                }
                $treeInfo .="</li>";
            }
        }
        return $treeInfo."</ul>";
    }
}

?>